import axios from "axios";
import { API, createApis } from "../fulfill";
import service from "@/config/index";

const CUSTOMER_SCHEMA = {
  addCustomer: {
    url: "/customer",
    methods: "POST"
  },
  updateCustomer: {
    url: "/customer/${customerId}",
    method: "PUT"
  },
  searchCustomer: {
    url: "/customer/search",
    method: "POST"
  },
  enableCustomer: {
    url: "/customer/enable",
    method: "PUT"
  },
  disableCustomer: {
    url: "/customer/disable",
    method: "PUT"
  },
  getCustomerById: {
    url: "/customer/${customerId}",
    method: "GET"
  },
  agreeCustomer: {
    url: "/customer/online/agree",
    method: "PUT"
  },
  rejectCustomer: {
    url: "/customer/online/reject",
    method: "PUT"
  },
  enableAutoBalance: {
    url: "/customer/online/balance/auto/enable",
    method: "PUT"
  },
  disableAutoBalance: {
    url: "/customer/online/balance/auto/disable",
    method: "PUT"
  },
  addOrderDefaults: {
    url: "/customer/order/defaults",
    method: "POST"
  },
  getOrderDefaults: {
    url: "/customer/order/defaults/${customerId}",
    method: "GET"
  }
};

const CONSIGNEE_SCHEMA = {
  addConsignee: {
    url: "/consignee",
    methods: "POST"
  },
  updateConsignee: {
    url: "/consignee/${consigneeId}",
    method: "PUT"
  },
  searchConsignee: {
    url: "/consignee/search",
    method: "POST"
  },
  getConsigneeById: {
    url: "/consignee/${consigneeId}",
    method: "GET"
  },
  getConsigneeEmailConfirm: {
    url: "/consignee/email/confirm/token/${emialToken}",
    method: "GET"
  },
};

const CARRIER_SCHEMA = {
  addCarrier: {
    url: "/carrier",
    methods: "POST"
  },
  updateCarrier: {
    url: "/carrier/${carrierId}",
    method: "PUT"
  },
  searchCarrier: {
    url: "/carrier/search",
    method: "POST"
  },
  getCarrierById: {
    url: "/carrier/${carrierId}",
    method: "GET"
  }
};

const DRIVER_SCHEMA = {
  searchDriver: {
    url: "/driver/search",
    method: "POST"
  }
};

const VEHICLE_SCHEMA = {
  searchVehicle: {
    url: "/vehicle/search",
    method: "POST"
  }
};

const TENANT_SCHEMA = {
  getCurrentTenant: new API({
    url: "",
    method: "GET"
  })
};

const USER_SCHEMA = {
  getCurrentUser: new API({
    url: "",
    method: "GET"
  })
};

const TENANT_APIS = createApis(service.serviceTenant, TENANT_SCHEMA);
const USER_APIS = createApis(service.serviceUser, USER_SCHEMA);
const CUSTOMER_APIS = createApis(service.serviceBasic, CUSTOMER_SCHEMA);
const CONSIGNEE_APIS = createApis(service.serviceBasic, CONSIGNEE_SCHEMA);
const CARRIER_APIS = createApis(service.serviceBasic, CARRIER_SCHEMA);
const DRIVER_APIS = createApis(service.serviceBasic, DRIVER_SCHEMA);
const VEHICLE_APIS = createApis(service.serviceBasic, VEHICLE_SCHEMA);

export default {
  loadDictionaries (filter) {
    let url = "/admin/dictionary/search";
    return axios.post(url, filter);
  },
  loadResources (filter) {
    let url = "/resource/search";
    return axios.post(url, filter);
  },
  loadCustomers (data) {
    return CUSTOMER_APIS.searchCustomer({ data });
  },
  loadConsignees (data) {
    return CONSIGNEE_APIS.searchConsignee({ data });
  },
  loadCarriers (data) {
    return CARRIER_APIS.searchCarrier({ data });
  },
  setCarrierAbbr ({ carrierId, abbr }) {
    return CARRIER_APIS.updateCarrier({ variables: { carrierId }, data: { abbr } });
  },
  loadDrivers (data) {
    return DRIVER_APIS.searchDriver({ data });
  },
  loadVehicles (data) {
    return VEHICLE_APIS.searchVehicle({ data });
  },
  saveCustomerConfig (data) {
    return CUSTOMER_APIS.addOrderDefaults({ data });
  },
  getConsigneeEmailConfirm (emialToken) {
    return CONSIGNEE_APIS.getConsigneeEmailConfirm({ variables: { emialToken } });
  },
};
